* Firm organization with multiple establishments
* Section III.C: Table VI; Appendix Tables B.19, B.21-23

clear all 
set matsize 4000
set more off

capture log close
log using log/14_facts-p_dynMEorg_layer-firm.log, replace

use data/MEorg_2000-2010_p_layer-firm_transition.dta, clear

********************************************************************************
***	TABLE VI: Transition dynamics of the managerial organization 

***	Upper panel: firm-level
tab2 count_mgmt_unt f_count_mgmt_unt if count_est > 1, m
tab2 count_mgmt_unt f_count_mgmt_unt if count_est > 1, m row nofreq

***	Lower panel: headquarters/establishment
tab2 organization f_organization if count_est > 1 & count_mgmt_bet1 >= count_mgmt_bet0, m
tab2 organization f_organization if count_est > 1 & count_mgmt_bet1 >= count_mgmt_bet0, m row nofreq

gen sameorg = (organization == f_organization)
tab jahr sameorg, m row
tab2 organization sameorg if count_est > 1, m 
tab2 organization sameorg if count_est > 1, row nofreq

***	Figures in text
count
count if count_est > 1
count if count_est > 1 & f_count_mgmt_bet0 != . & f_count_mgmt_bet1 != .
count if count_est > 1 & count_mgmt_bet0 != f_count_mgmt_bet0 & f_count_mgmt_bet0 != . & f_count_mgmt_bet1 != .
count if count_est > 1 & count_mgmt_bet1 != f_count_mgmt_bet1 & f_count_mgmt_bet0 != . & f_count_mgmt_bet1 != .

count if count_est > 1 & (count_mgmt_bet0 != f_count_mgmt_bet0 | count_mgmt_bet1 != f_count_mgmt_bet1) & f_count_mgmt_bet0 != . & f_count_mgmt_bet1 != .
count if count_est > 1 & count_mgmt_bet0 != f_count_mgmt_bet0 & count_mgmt_bet1 != f_count_mgmt_bet1 & f_count_mgmt_bet0 != . & f_count_mgmt_bet1 != . // change at both
count if count_est > 1 & count_mgmt_bet0 == f_count_mgmt_bet0 & count_mgmt_bet1 != f_count_mgmt_bet1 & f_count_mgmt_bet0 != . & f_count_mgmt_bet1 != . // change only at HQ
count if count_est > 1 & count_mgmt_bet0 != f_count_mgmt_bet0 & count_mgmt_bet1 == f_count_mgmt_bet1 & f_count_mgmt_bet0 != . & f_count_mgmt_bet1 != . // change only at establishment

********************************************************************************
***	Table B.19: Size at transition 

gen ln_empl_unt = ln(empl_unt)
gen ln_sales = ln(Sales)

***	Upper panel
cap drop cellsize
bys count_mgmt_unt f_count_mgmt_unt: egen cellsize = count(count_mgmt_unt) if count_est > 1
table count_mgmt_unt f_count_mgmt_unt if count_est > 1 & cellsize > 49, contents(count ln_empl_unt mean ln_empl_unt) format(%9.1f)
table count_mgmt_unt f_count_mgmt_unt if count_est > 1 & cellsize > 49, contents(count ln_sales mean ln_sales) format(%9.1f)

foreach outcome in ln_empl_unt ln_sales {
	display as text "********************************************************************************"
	display as text "*** Outcome: `outcome' *********************************************************"

	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 0 & f_count_mgmt_unt == "0"	| count_mgmt_unt == 0 & f_count_mgmt_unt == "1"), unequal by(f_count_mgmt_unt)
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 0 & f_count_mgmt_unt == "0"	| count_mgmt_unt == 0 & f_count_mgmt_unt == "2"), unequal by(f_count_mgmt_unt)
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 0 & f_count_mgmt_unt == "0"	| count_mgmt_unt == 0 & f_count_mgmt_unt == "SE"), unequal by(f_count_mgmt_unt)
	
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 1 & f_count_mgmt_unt == "1"	| count_mgmt_unt == 1 & f_count_mgmt_unt == "0"), unequal by(f_count_mgmt_unt)
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 1 & f_count_mgmt_unt == "1"	| count_mgmt_unt == 1 & f_count_mgmt_unt == "2"), unequal by(f_count_mgmt_unt)
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 1 & f_count_mgmt_unt == "1"	| count_mgmt_unt == 1 & f_count_mgmt_unt == "SE"), unequal by(f_count_mgmt_unt)
	
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 2 & f_count_mgmt_unt == "2"	| count_mgmt_unt == 2 & f_count_mgmt_unt == "1"), unequal by(f_count_mgmt_unt)
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 2 & f_count_mgmt_unt == "2"	| count_mgmt_unt == 2 & f_count_mgmt_unt == "3"), unequal by(f_count_mgmt_unt)
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 2 & f_count_mgmt_unt == "2"	| count_mgmt_unt == 2 & f_count_mgmt_unt == "SE"), unequal by(f_count_mgmt_unt)
	
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 3 & f_count_mgmt_unt == "3"	| count_mgmt_unt == 3 & f_count_mgmt_unt == "2"), unequal by(f_count_mgmt_unt)
	ttest `outcome' if count_est > 1 & (count_mgmt_unt == 3 & f_count_mgmt_unt == "3"	| count_mgmt_unt == 3 & f_count_mgmt_unt == "SE"), unequal by(f_count_mgmt_unt)
}

***	Lower panel
cap drop cellsize
bys organization f_organization: egen cellsize = count(organization) if count_est > 1 & count_mgmt_bet1 >= count_mgmt_bet0
table organization f_organization if count_est > 1 & count_mgmt_bet1 >= count_mgmt_bet0 & cellsize > 49, contents(count ln_empl_unt mean ln_empl_unt) format(%9.1f)
table organization f_organization if count_est > 1 & count_mgmt_bet1 >= count_mgmt_bet0 & cellsize > 49, contents(count ln_sales mean ln_sales) format(%9.1f)

foreach outcome in ln_empl_unt ln_sales {
	display as text "********************************************************************************"
	display as text "*** Outcome: `outcome' *********************************************************"

	ttest `outcome' if count_est > 1 & (organization == "0/0" 	& f_organization == "0/0" 	| organization == "0/0"	 & f_organization == "1/<1"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "0/0" 	& f_organization == "0/0" 	| organization == "0/0"	 & f_organization == "SE"), unequal by(f_organization)
	
	ttest `outcome' if count_est > 1 & (organization == "1/<1" 	& f_organization == "1/<1" 	| organization == "1/<1" & f_organization == "0/0"),  unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "1/<1" 	& f_organization == "1/<1" 	| organization == "1/<1" & f_organization == "1/1"),  unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "1/<1" 	& f_organization == "1/<1" 	| organization == "1/<1" & f_organization == "2/<2"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "1/<1" 	& f_organization == "1/<1" 	| organization == "1/<1" & f_organization == "SE"), unequal by(f_organization)
	
	ttest `outcome' if count_est > 1 & (organization == "1/1" 	& f_organization == "1/1" 	| organization == "1/1"  & f_organization == "1/<1"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "1/1" 	& f_organization == "1/1" 	| organization == "1/1"  & f_organization == "2/<2"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "1/1" 	& f_organization == "1/1" 	| organization == "1/1"  & f_organization == "SE"), unequal by(f_organization)
	
	ttest `outcome' if count_est > 1 & (organization == "2/<2" 	& f_organization == "2/<2" 	| organization == "2/<2" & f_organization == "1/<1"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "2/<2" 	& f_organization == "2/<2" 	| organization == "2/<2" & f_organization == "1/1"),  unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "2/<2" 	& f_organization == "2/<2" 	| organization == "2/<2" & f_organization == "2/2"),  unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "2/<2" 	& f_organization == "2/<2" 	| organization == "2/<2" & f_organization == "3/<3"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "2/<2" 	& f_organization == "2/<2" 	| organization == "2/<2" & f_organization == "SE"), unequal by(f_organization)
	
	ttest `outcome' if count_est > 1 & (organization == "2/2" 	& f_organization == "2/2" 	| organization == "2/2"  & f_organization == "2/<2"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "2/2" 	& f_organization == "2/2" 	| organization == "2/2"  & f_organization == "3/<3"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "2/2" 	& f_organization == "2/2" 	| organization == "2/2"  & f_organization == "SE"), unequal by(f_organization)
	
	ttest `outcome' if count_est > 1 & (organization == "3/<3" 	& f_organization == "3/<3" 	| organization == "3/<3" & f_organization == "2/<2"), unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "3/<3" 	& f_organization == "3/<3" 	| organization == "3/<3" & f_organization == "2/2"),  unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "3/<3" 	& f_organization == "3/<3" 	| organization == "3/<3" & f_organization == "3/3"),  unequal by(f_organization)
	ttest `outcome' if count_est > 1 & (organization == "3/<3" 	& f_organization == "3/<3" 	| organization == "3/<3" & f_organization == "SE"),  unequal by(f_organization)
	
	ttest `outcome' if count_est > 1 & (organization == "3/3" 	& f_organization == "3/3" 	| organization == "3/3"  & f_organization == "3/<3"), unequal by(f_organization)
}

********************************************************************************
***	Table B.21: Long differences 

***	Upper panel: firm-level
tab2 count_mgmt_unt f5_count_mgmt_unt if count_est > 1
tab2 count_mgmt_unt f5_count_mgmt_unt if count_est > 1, row nofreq

***	Lower panel: headquarters/establishment
tab2 organization f5_organization if count_est > 1 & count_mgmt_bet1 >= count_mgmt_bet0,
tab2 organization f5_organization if count_est > 1 & count_mgmt_bet1 >= count_mgmt_bet0, row nofreq

count
count if count_est > 1
count if count_est > 1 & f5_count_mgmt_bet0 != . & f5_count_mgmt_bet1 != .

count if count_est > 1 & (count_mgmt_bet0 != f5_count_mgmt_bet0 | count_mgmt_bet1 != f5_count_mgmt_bet1) & f5_count_mgmt_bet0 != . & f5_count_mgmt_bet1 != .
count if count_est > 1 & count_mgmt_bet0 != f5_count_mgmt_bet0 & count_mgmt_bet1 != f5_count_mgmt_bet1 & f5_count_mgmt_bet0 != . & f5_count_mgmt_bet1 != . // change at both
count if count_est > 1 & count_mgmt_bet0 == f5_count_mgmt_bet0 & count_mgmt_bet1 != f5_count_mgmt_bet1 & f5_count_mgmt_bet0 != . & f5_count_mgmt_bet1 != . // change only at HQ
count if count_est > 1 & count_mgmt_bet0 != f5_count_mgmt_bet0 & count_mgmt_bet1 == f5_count_mgmt_bet1 & f5_count_mgmt_bet0 != . & f5_count_mgmt_bet1 != . // change only at establishment

********************************************************************************
***	Table B.22: By # establishments 

tabstat count_est, s(n mean sd min p5 p10 q p90 p95 max)

preserve
keep if count_est == 2

tab2 count_mgmt_unt f_count_mgmt_unt, m
tab2 count_mgmt_unt f_count_mgmt_unt, m row nofreq
tab2 organization f_organization, m
tab2 organization f_organization, m row nofreq
restore

preserve
keep if count_est > 2

tab2 count_mgmt_unt f_count_mgmt_unt, m
tab2 count_mgmt_unt f_count_mgmt_unt, m row nofreq
tab2 organization f_organization, m
tab2 organization f_organization, m row nofreq
restore

********************************************************************************
***	Table B.23: By median maximum distance to HQ 

replace max_dist = . if count_est == 1
tabstat max_dist if count_est > 1, s(n mean sd min p5 p10 q p90 p95 max)

preserve
qui sum max_dist, det
keep if max_dist <= r(p50)

tab2 count_mgmt_unt f_count_mgmt_unt, m
tab2 count_mgmt_unt f_count_mgmt_unt, m row nofreq
tab2 organization f_organization, m
tab2 organization f_organization, m row nofreq
restore

preserve
qui sum max_dist, det
keep if max_dist > r(p50) & max_dist != .

tab2 count_mgmt_unt f_count_mgmt_unt, m
tab2 count_mgmt_unt f_count_mgmt_unt, m row nofreq
tab2 organization f_organization, m
tab2 organization f_organization, m row nofreq
restore

erase "data/MEorg_2000-2010_p_layer-firm_transition.dta"

log close
